<?php
	include 'config.php';
	include 'flag.php';
	include 'app/function.php';
	if(IS_INSTALL){
		echo '<script>alert("你已经成功安装，请从前台登录！");location.href="/"</script>';
		exit;
	}
	if($_POST['sub_info']){
		$unit_name = $_REQUEST['unit_name'];
		$admin_name = $_REQUEST['admin_name'];
		$admin_pwd = $_REQUEST['admin_pwd'];
		$db_ip = $_REQUEST['db_ip'];
		$db_name = $_REQUEST['db_name'];
		$db_username = $_REQUEST['db_username'];
		$db_pwd = $_REQUEST['db_pwd'];
		$ftp_name = $_REQUEST['ftp_ip'];
		$ftp_username = $_REQUEST['ftp_username'];
		$ftp_pwd = $_REQUEST['ftp_pwd'];
		$ftp_path = $_REQUEST['ftp_path'];
		if(empty($unit_name)||empty($admin_name)||empty($admin_pwd)||empty($db_ip)||empty($db_name)||empty($db_username)||empty($db_pwd)){
			$error_msg = '信息输入不完整，请重新输入！';
		}else{
			$error_msg = false;
		}
	}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>CSM安装程序</title>
</head>
<script type='text/javascript' src='/js/jquery.js'></script>
<style>
table{
	width:80%;
	min-width:300px;
	background:#eee;
}
tr{
	height:30px;
}
font{
	font-size:12px;
}
button{
	width:80px;
	height:30px;
	color:red;
}
.gray{
	color:#999;
	font-size:12px;
}
</style>
<body>
<?php 
	if(isset($error_msg) && !$error_msg){
		$i = 1;
?>
	<div id="show_update" style="padding:10px 30px 10px 30px;text-align:left;width:500px;height:300px;overflow:hidden;background:#eee;margin: auto;line-height:30px;">
		<div style="width:50%;margin:auto;text-align:center;color:orange;font-size:20px;line-height:40px;">安装进度</div>
		<hr style="width:100%;color:#cccccc"></hr>
		<span id='install_info'>
		<?php 
			if(set_webtitle($unit_name)){
				echo $i++."、修改网站标题&nbsp;(<span style='color:green;font-size:12pz;'>成功</span><span>)<br>";
			}else{
				echo $i++."、修改网站标题&nbsp;<span style='color:red;font-size:12pz;'>失败</span><span>)，请检查文件是否有写入权限!（文件：JYCsm/tpl/main/index.php）<br>";
				exit;
			}
			if(set_ocx($ftp_name,$ftp_username,$ftp_pwd,$ftp_path)){
				echo $i++."、修改ocx文件&nbsp;(<span style='color:green;font-size:12pz;'>成功</span><span>)<br>";
			}else{
				echo $i++."、修改ocx文件&nbsp;<span style='color:red;font-size:12pz;'>失败</span><span>)，请检查文件是否有写入权限!（文件：JYCsm/web/js/ocx.js）<br>";
				exit;
			}
			if(set_defaultdb($db_ip,$db_name,$db_username,$db_pwd)){
				echo $i++."、修改数据库配置文件&nbsp;(<span style='color:green;font-size:12pz;'>成功</span><span>)<br>";
			}else{
				echo $i++."、修改数据库配置文件&nbsp;<span style='color:red;font-size:12pz;'>失败</span><span>)，请检查文件是否有写入权限!（文件：JYAPI/config/defaultDB.yml）<br>";
				exit;
			}
		?>
		</span>
	</div>
	<script type='text/javascript'>
		var i=0,msg=''
		var wait = ['.','..','...','....','.....','......','.......','........']
		var error_msg={'fail_key':'校验码错误','suc':'成功','fail':'系统繁忙'}
		$(window).ready(function(){
			$('div#show_update').find('span#install_info').append('<span id="install_db"><?php echo $i++;?>、正在安装数据库&nbsp;<span id="install_wait"></span><span>')
			var cy1 = setInterval(function(){
					if(i<8){
						$('span#install_wait').html(wait[i++])
					}else{
						i=0
						$('span#install_wait').html('')
					}
				},500)
			$.ajax({
				url:'app/do.php',
				data:{'m':1,'k':'<?php echo md5('do.php1nykj');?>'},
				type:'post',
				dataType:'json',
				success:(function(de){
					window.clearInterval(cy1)
					if(de){
						switch(de.code){
							case 1:msg='<font color="green">成功</font>';break;
							case -1:msg='<font color="red">'+error_msg[de.msg]+'</font>';break;
							dufault:msg=error_msg['fail']
						}
						$('span#install_wait').html('&nbsp;('+msg+')')
						if(de.code==1){
							initialize_db();
						}else{
							$('div#show_update').append('<button>安装失败</button>')
						}
					}else{
						$('span#install_wait').html('失败')
					}
				})
			})
		})
		
		function initialize_db(){
			$('div#show_update').find('span#install_info').append('<br><span id="initialize_db"><?php echo $i++;?>、初始化数据库&nbsp;<span id="initialize_wait"></span><span>')
			i=0
			var cy2 = setInterval(function(){
					if(i<8){
						$('span#initialize_wait').html(wait[i++])
					}else{
						i=0
						$('span#initialize_wait').html('')
					}
				},500);
			$.ajax({
				url:'app/do.php',
				data:{'m':2,'k':'<?php echo md5('do.php2nykj');?>','un':'<?php echo $unit_name;?>','an':'<?php echo $admin_name;?>','ap':'<?php echo $admin_pwd;?>'},
				type:'post',
				dataType:'json',
				success:(function(de){
					window.clearInterval(cy2)
					if(de){
						switch(de.code){
							case 1:msg='<font color="green">成功</font>';break;
							case -1:msg='<font color="red">'+error_msg[de.msg]+'</font>';break;
							dufault:msg=error_msg['fail']
						}
						$('span#initialize_wait').html('&nbsp;('+msg+')')
						if(de.code==1){
							$('div#show_update').append('<br><button onclick="location.href=\'/\'">安装成功,返回首页</button>')
						}else{
							$('div#show_update').append('<br><button>安装失败</button>')
						}
					}else{
						$('span#initialize_wait').html('失败')
					}
				})
			})
		}
	</script>
<?php 
	}else{
?>
<form action='' method='post'>
	<table align='center' cellspacing='1' >
		<tr><td height="40px" colspan="10" align="center"><strong>CSM安装系统</strong></td></tr>
		<tr>
			<td align='right' width="45%">医院名称：</td>
			<td><input type='text' name='unit_name' value='<?php echo $unit_name;?>'></input></td>
		</tr>
		<tr>
			<td align='right'>管理员登录用户名：</td>
			<td><input type='text' name='admin_name' value='<?php echo $admin_name?$admin_name:'admin';?>'></input></td>
		</tr>
		<tr>
			<td align='right'>管理员登录密码：</td>
			<td><input type='text' name='admin_pwd' value='<?php echo $admin_pwd?$admin_pwd:'120120';?>'></input></td>
		</tr>
		<tr>
			<td align='right'>数据库地址(IP地址)：</td>
			<td><input type='text' name='db_ip' value='<?php echo $db_ip?$db_ip:'127.0.0.1';?>'></input></td>
		</tr>
		<tr>
			<td align='right'>数据库名称：</td>
			<td><input type='text' name='db_name' value='<?php echo $db_name?$db_name:'orcl';?>'></input></td>
		</tr>
		<tr>
			<td align='right'>数据库登录用户名：</td>
			<td><input type='text' name='db_username' value='<?php echo $db_username?$db_username:'csm';?>'></input></td>
		</tr>
		<tr>
			<td align='right'>数据库登录密码：</td>
			<td><input type='text' name='db_pwd' value='<?php echo $db_pwd?$db_pwd:'nykj_csm';?>'></input></td>
		</tr>
		<tr>
			<td align='right'>FTP服务器地址：</td>
			<td><input type='text' name='ftp_ip' value='<?php echo $ftp_name?$ftp_name:'127.0.0.1';?>'></input>&nbsp;<span class='gray'>(请确认该目录下是否另建csmftp文件夹)</span></td>
		</tr>
		<tr>
			<td align='right'>FTP登录用户名：</td>
			<td><input type='text' name='ftp_username' value='<?php echo $ftp_username?$ftp_username:'csm';?>'></input></td>
		</tr>
		<tr>
			<td align='right'>FTP登录密码：</td>
			<td><input type='text' name='ftp_pwd' value='<?php echo $ftp_pwd?$ftp_pwd:'nykj_csm';?>'></input></td>
		</tr>
		<tr>
			<td align='right'>FTP本地存储路径：</td>
			<td><input type='text' name='ftp_path' value='<?php echo $ftp_path?$ftp_path:'D:/nycsm/';?>'></input>&nbsp;<span class='gray'>(请确认该目录下是否另建records文件夹)</span></td>
		</tr>
		<tr>
			<td align='right'>&nbsp;</td>
			<td>
				<input type='submit' name='sub_info' value='提交'></input>
				<input type='reset' value='重置'></input><br></br>
				<span id='error_msg' style='color:red;font-size:12px;'><?php echo $error_msg;?></span>
			</td>
		</tr>
	</table>
</form>
<?php }?>
</body>
</html>
<script type='text/javascript'>
setInterval(function(){$('span#error_msg').html('')},3000)
</script>